package com.firebird.app.kotlin.stock.trend.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.firebird.app.kotlin.stock.trend.R
import com.firebird.app.kotlin.stock.trend.StockApp
import com.firebird.app.kotlin.stock.trend.adapter.StockBaseAdapter
import com.firebird.app.kotlin.stock.trend.event.BaseEvent
import com.firebird.app.kotlin.stock.trend.model.StockListPlateResult
import com.firebird.app.kotlin.stock.trend.model.StockPlate
import com.firebird.app.kotlin.stock.trend.utils.Bus
import com.firebird.app.kotlin.stock.trend.utils.Log
import com.firebird.app.kotlin.stock.trend.utils.NetworkUtil
import com.firebird.app.kotlin.stock.trend.utils.net.volley.Listener
//import com.firebird.app.kotlin.stock.trend.utils.net.volley.StockListener
import com.firebird.app.kotlin.stock.trend.utils.net.volley.add
import com.firebird.app.kotlin.stock.trend.utils.net.volley.toString
import kotlinx.android.synthetic.main.fragment_stock_list_plate.*
import kotlinx.android.synthetic.main.item_list_stock_plate.view.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.sdk25.coroutines.onLongClick
import org.jetbrains.anko.support.v4.act
import org.jetbrains.anko.support.v4.alert
import org.jetbrains.anko.support.v4.toast

/**
 * Created by ljk on 19/12/31.
 */
class StockListPlateFragment : BaseFragment() {
    override val layoutResourceId: Int = R.layout.fragment_stock_list_plate
    var result: StockListPlateResult? = null
    var adapter: StockBaseAdapter<StockPlate>? = null

    companion object {
        fun getInstance(): StockListPlateFragment {
            return StockListPlateFragment()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val layoutManager: LinearLayoutManager = LinearLayoutManager(act)
        stockListPlate.layoutManager = layoutManager
    }

    private fun itemLongClick(item: StockPlate): Boolean {
        toast(item.code)
        return true
    }

    override fun onResume() {
        super.onResume()
        init()
    }

    fun init() {
        if (NetworkUtil.isNetwork(act)) {
            val listener = Listener<StockListPlateResult> { e, r ->
                e?.let { Bus.post(BaseEvent<StockListPlateResult>(error = e)) }
                r?.let { Bus.post(BaseEvent(response = r)) }
            }
            val url = resources.getString(R.string.gank_list)
            Log.d("Url:::", url)
            StockApp.requestQueue!!.add(listener, url)
            //loading(Const.SHOW)
            toast("正在加载中...")
        } else {
            alert("网络错误", "网络未连接，请检查网络")
        }
    }

    fun onEventMainThread(event: BaseEvent<StockListPlateResult>) {
        //loading(Const.HIDE)
        val error = event.error
        result = event.response
        //info(result)
        toast("加载完成!!!")
        if (error != null) {
            toast(error.toString(resources))
        } else {
            if (result!!.error) {
                alert("网络错误", "网络未连接，请检查网络")
            } else {
                adapter = StockBaseAdapter(R.layout.item_list_stock_plate, result!!.results) { view: View, item: StockPlate ->
                    view.item_stock_code.text = item.code
                    view.item_stock_name.text = item.name
                    view.item_stock_nowPri.text = item.nowPri
                    view.item_stock_increase.text = item.increase
                    view.item_stock_increPer.text = item.increPer
                    view.onClick {
                        toast("onclick")
                    }
                    view.onLongClick {
                        itemLongClick(item)
                    }
                }
                stockListPlate.adapter = adapter
                adapter!!.notifyDataSetChanged()
            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
    }


}